﻿using CameraParentClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CameraLibrary
{
    internal class HikVision : CameraParent
    {
        private int _delayTime;
        /// <summary>
        /// 延时
        /// </summary>
        public override int DelayTime
        {
            get => _delayTime;
            set => _delayTime = value;
        }
        
        /// <summary>
        /// 延时时间
        /// </summary>
        Task task = null;
        CancellationTokenSource cts = null;
        public HikVision()
        {
            BrandName = "海康威视";
        }

        public override void OpenCamera()
        {
            base.OpenCamera();
            SetLabelText?.Invoke("打开相机");
            cts = new CancellationTokenSource();
            task = new Task(DoWork);
            task.Start();
        }
        public void DoWork()
        {
            while (true)
            {
                switch (CameraWorkState)
                {
                    case CamerWork.None:
                        break;
                    case CamerWork.采集一张:
                        SetLabelText?.Invoke(DateTime.Now.ToString());                        
                        CameraWorkState = CamerWork.None;
                        break;
                    case CamerWork.连续采集:
                        SetLabelText?.Invoke(DateTime.Now.ToString());
                        break;
                }
                Thread.Sleep(DelayTime);
                //判断如果收到了线程取消的通知就退出循环
                if (cts.Token.IsCancellationRequested)
                {
                    break;
                }
            }
        }

        public override void CloseCamera()
        {
            base.CloseCamera();
            SetLabelText?.Invoke("关闭相机");
            cts.Cancel();
        }

    }
}
